package com.czkj.hwz.decorate.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.czkj.hwz.decorate.IMP.HttpDoImp;
import com.czkj.hwz.decorate.adapter.GoodsAdapter;
import com.czkj.hwz.decorate.base.BaseFragment;
import com.czkj.hwz.decorate.base.HttpUrl;
import com.czkj.hwz.decorate.bean.Enterprise;
import com.czkj.hwz.decorate.bean.Goods;
import com.czkj.hwz.decorate.bean.MyTab;
import com.czkj.hwz.decorate.control.GoodsGet;
import com.czkj.hwz.decorate.control.IndexGet;
import com.czkj.hwz.decorate.R;
import com.joanzapata.android.BaseAdapterHelper;
import com.joanzapata.android.QuickAdapter;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MsgFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
@ContentView(R.layout.fragment_msg)
public class MsgFragment extends BaseFragment implements HttpDoImp {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private IndexGet indexGet;
    private ArrayList<Enterprise> mData;
    private ArrayList<Goods> mGoods;
    GoodsGet mGoodsGet;
    private GoodsAdapter goodsAdapter;

    @ViewInject(R.id.ll)
    LinearLayout ll;
    @ViewInject(R.id.ll_h)
    LinearLayout ll_h;

    @ViewInject(R.id.tv_qb)
    TextView tv_qb;
    @ViewInject(R.id.gv_qb)
    GridView gv_qb;
    MyTab mMyTab_v,mMyTab_h;
    private int H_index=0,V_index=-1;
    public MsgFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MsgFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MsgFragment newInstance(String param1, String param2) {
        MsgFragment fragment = new MsgFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);




//        mMyTab_v = new MyTab(getContext(),LinearLayout.VERTICAL) {
//            @Override
//            public void onItemClickListener(int position) {
//                V_index = position;
//            }
//        };
//        ArrayList<String> data = new ArrayList<>();
//        for (int i = 0; i <20 ; i++) {
//            data.add("这是标题:"+i);
//        }
//        mMyTab_v.setData(data);
//        mMyTab_v.setText_Qb(tv_qb);
//        ll.addView(mMyTab_v.getView());
//        tv_qb.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                mMyTab_v.changeColor(-1);
//                V_index = - 1 ;
//                tv_qb.setTextColor(getResources().getColor(R.color.yellow));
//            }
//        });
//
//
//
//
//        mMyTab_h = new MyTab(getContext(),LinearLayout.HORIZONTAL) {
//            @Override
//            public void onItemClickListener(int poistion) {
//                H_index = poistion;
//
//
//            }
//        };



//        //默认 全部商品选中；横向选择第一个；
//        mMyTab_v.changeColor(-1);
//        V_index = - 1 ;
//        tv_qb.setTextColor(getResources().getColor(R.color.yellow));
//        H_index = 0;
//        mMyTab_h.changeColor(0);


    }

    @Override
    protected void lazyLoad() {
        indexGet=new IndexGet(getContext(),this);
        indexGet.getIndex(null);
    }

    @Override
    public void onSuccess(String flag, Object... objects) {
        switch (flag) {
            case "indexGet":
                mData= (ArrayList<Enterprise>) objects[2];
                mMyTab_h=new MyTab(getContext(),LinearLayout.HORIZONTAL) {
                    @Override
                    public void onItemClickListener(int poistion) {
                        int id=mData.get(poistion).getId();
                        mGoodsGet=new GoodsGet(getContext(),MsgFragment.this);
                        HashMap<String, String> reqParams = new HashMap<>();
                        reqParams.put("enterpriseid", String.valueOf(id));
                        mGoodsGet.getList(reqParams);
                    }
                };
                mMyTab_h.setData(mData);
                ll_h.addView(mMyTab_h.getView());
                break;
            case "getList":
                mGoods= (ArrayList<Goods>) objects[1];
                //goodsAdapter=new GoodsAdapter(getContext(),mGoods);
                //gv_qb.setAdapter(goodsAdapter);
                gv_qb.setAdapter(new QuickAdapter<Goods>(getContext(),R.layout.grideview_goods_item,mGoods) {
                    @Override
                    protected void convert(BaseAdapterHelper helper, Goods item) {
                        helper.setText(R.id.tv_title,item.getName());
                        ImageView img = helper.getView(R.id.img);
                        x.image().bind(img, HttpUrl.BASE_URL+item.getSmallImg());
                    }
                });
                break;
        }
    }

    @Override
    public void onError(String errinfo) {

    }
}
